﻿-- =============================================
-- Populates the SecurityRoles & SecurityRoleAccess Tables
-- =============================================

IF '$(StaticData)' = '1'
	PRINT 'Creating/Updating SecurityRole and SecurityRoleAccess Tables...'
GO

IF '$(StaticData)' = '1'
BEGIN
	SET NOCOUNT ON
	SET IDENTITY_INSERT dbo.SecurityRole ON

	DECLARE @RoleID INT,
		@RoleName NVARCHAR(100)

	SELECT @RoleID = 0
		, @RoleName = 'Administrators'

	IF NOT EXISTS (SELECT 1 FROM dbo.SecurityRole WHERE SRO_SecurityRoleID = @RoleID)
		INSERT INTO dbo.SecurityRole(SRO_SecurityRoleID, SRO_Name, SRO_BaseSecurityRoleID, SRO_LastModifiedBy)
		VALUES (@RoleID, @RoleName, NULL, 'System')

	--Add all Permissions that don't already belong to the Administrator Role to the Role (this is the only role we can do this with)
	INSERT INTO dbo.SecuirtyRoleAccess(SRA_SecurityAccessID, SRA_SecurityRoleID)
	SELECT SAC_SecurityAccessID, @RoleID 
	FROM dbo.SecurityAccess 
	LEFT OUTER JOIN dbo.SecuirtyRoleAccess 
	 ON SRA_SecurityAccessID = SAC_SecurityAccessID 
	WHERE SRA_SecurityRoleAccessID IS NULL


	SET IDENTITY_INSERT dbo.SecurityRole OFF
	SET NOCOUNT OFF
END